############################
## BLIS/Swayable: Table 1 ##
############################

# File Description: 
  # Uses data/packages from O_Data.R
  # Replicates Table 1 from Fabric of Repair report (2025)
# Date Last Updated: 2 May 2025

############################

# Manipulate the descriptive variables of interest into a new dataframe
df_desc_table <- df %>%
  mutate(Race = race,
         `College-Educated` = education_binary,
         Income = case_when(What.is.the.total.annual.income.of.your.household.before.taxes. == "100-150-k" ~ "$100-150K",
                            What.is.the.total.annual.income.of.your.household.before.taxes. == "under-20-k" ~ "Under $20K",
                            What.is.the.total.annual.income.of.your.household.before.taxes. == "20-40-k" | What.is.the.total.annual.income.of.your.household.before.taxes. == "40-60k" ~ "$20-60K",
                            What.is.the.total.annual.income.of.your.household.before.taxes. == "60-80-k" | What.is.the.total.annual.income.of.your.household.before.taxes. == "80-100-k" ~ "$60-100K",
                            What.is.the.total.annual.income.of.your.household.before.taxes. == "150-200-k" | What.is.the.total.annual.income.of.your.household.before.taxes. == "over-200-k" ~ "Over $150K",
                            TRUE ~ NA),
         Income = factor(Income, levels = c("Under $20K", "$20-60K", "$60-100K", "$100-150K", "Over $150K")),
         `Party Affiliation` = case_when(Party == "democrat" ~ "Democrat",
                                         Party == "republican" ~ "Republican",
                                         TRUE ~ "Independent or Something Else"),
         Residence = case_when(Area == "city" ~ "City",
                               Area == "in-a-rural-area" ~ "Rural",
                               Area == "small-town-next-to-native-community-e-g-reservation" |
                                 Area == "small-town-not-next-to-native-community-e-g-reservation" ~ "Small Town",
                               TRUE ~ "Another location"),
         Residence = factor(Residence, levels = c("City", "Small Town", "Rural", "Another Location")),
         Region = case_when(Region..Midwest == T ~ "Midwest",
                            Region..Northeast == T ~ "Northeast",
                            Region..South == T ~ "South",
                            Region..West == T ~ "West"),
         `Religious Affiliation` = case_when(
           What.is.your.present.religion..if.any. == "atheist" ~ "Atheist",
           
           What.is.your.present.religion..if.any. == "catholic" ~ "Catholic",
           What.is.your.present.religion..if.any. == "evangelical-protestant" ~ "Evangelical Protestant",
           
           What.is.your.present.religion..if.any. == "mormon" ~ "Mormon",
           What.is.your.present.religion..if.any. == "muslim" ~ "Muslim",
           
           What.is.your.present.religion..if.any. == "other-protestant" ~ "Other Protestant",
           What.is.your.present.religion..if.any. == "nothing-in-particular" ~ "Not Religious",
           TRUE ~ "Other Religion"
         ),
         Religion = factor(`Religious Affiliation`, levels = c("Not Religious", "Atheist", "Catholic", "Evangelical Protestant", "Other Protestant", "Muslim",  "Other Religion")),
         `Religious Affiliation` = ifelse(Religion == "Not Religious", "Not Religious", "Religious")) %>%
  select(Age, Gender, Race, `College-Educated`, Income, `Party Affiliation`, Residence, Region, `Religious Affiliation`)

# Select functions used for the `arsenal` table
my_controls <- tableby.control(
  test = F,
  total = T,
  numeric.test = "kwt", cat.test = "chisq",
  numeric.stats = c("meansd"),
  cat.stats = c("countpct"),
  stats.labels = list(
    meansd = "Mean (SD)"
  )
)

# Create the table
desc_table <- tableby(Race ~ ., data = df_desc_table, control = my_controls)

# To see the table in LaTeX, run this code in a .rmd or .qmd file and print to PDF/html
summary(desc_table)

